package com.jiukuaidao.zhulin.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.entity.ApiUserInfo;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/27
 * Description :
 */
public class UserInfoUtil {

    public static final String APPKEY_ZERO = "000000000000000";

    /**
     * 保存用户信息
     *
     * @param userInfo
     */
    public static void saveLoginInfo(ApiUserInfo userInfo) {
        SharedPreferences.Editor editor = AppContext.getContext().getSharedPreferences("userInfo", 0).edit();
        editor.putString("userId", userInfo.userId);
        editor.putString("headerImage", userInfo.headerImage);
        editor.putString("userCode", userInfo.userCode);
        editor.putString("userName", userInfo.userName);
        editor.putString("mobile", userInfo.mobile);
        editor.putInt("isSetpass", userInfo.isSetpass);

        editor.putInt("refundsCount",userInfo.refundsCount);
        editor.putInt("toBeEvaluatedCount",userInfo.toBeEvaluatedCount);
        editor.putInt("toBePaidCount",userInfo.toBePaidCount);
        editor.putInt("toBeRecivedCount",userInfo.toBeRecivedCount);

        editor.commit();
    }

    /**
     * 刷新本地token
     *
     * @param userCode
     */
    public static void refreshUserCode(String userCode) {
        SharedPreferences.Editor editor = AppContext.getContext().getSharedPreferences("userInfo", 0).edit();
        editor.putString("userCode", userCode);
        editor.commit();
    }

    /**
     * 获取本地token
     *
     * @return
     */
    public static String getUserCode() {
        SharedPreferences userInfo = AppContext.getContext().getSharedPreferences("userInfo", 0);
        String userCode = userInfo.getString("userCode", "");
        return userCode;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static ApiUserInfo getLoginInfo() {

        ApiUserInfo userInfo = new ApiUserInfo();
        SharedPreferences userData = AppContext.getContext().getSharedPreferences("userInfo", 0);
        userInfo.userId = userData.getString("userId", "");
        userInfo.headerImage = userData.getString("headerImage", "");
        userInfo.userCode = userData.getString("userCode", "");
        userInfo.userName = userData.getString("userName", "");
        userInfo.mobile = userData.getString("mobile", "");
        userInfo.isSetpass = userData.getInt("isSetPass", -1);

        userInfo.refundsCount = userData.getInt("refundsCount",0);
        userInfo.toBeEvaluatedCount = userData.getInt("toBeEvaluatedCount",0);
        userInfo.toBePaidCount = userData.getInt("toBePaidCount",0);
        userInfo.toBeRecivedCount = userData.getInt("toBeRecivedCount",0);

        return userInfo;
    }

    /**
     * 清除用户信息
     */
    public static void cleanLoginInfo() {
        SharedPreferences.Editor userData = AppContext.getContext().getSharedPreferences("userInfo", 0).edit();
        userData.clear();
        userData.commit();
    }

    /**
     * 设置本地登录状态
     *
     * @param isLogin true：登录 false：注销
     */
    public static void setLogin(boolean isLogin) {
        SharedPreferences.Editor editor = AppContext.getContext().getSharedPreferences("userInfo", 0).edit();
        editor.putInt("isLogin", isLogin ? 1 : 0);
        if (!isLogin) {
            cleanLoginInfo();
        }
        editor.commit();
    }

    /**
     * 是否登录
     */
    public static boolean isLogin() {
        ApiUserInfo userInfo = new ApiUserInfo();
        SharedPreferences userData = AppContext.getContext().getSharedPreferences("userInfo", 0);
        userInfo.isLogin = userData.getInt("isLogin", 0);
        if (userInfo.isLogin == 1) {
            return true;
        }
        return false;
    }

    /**
     * 唯一的设备ID： GSM手机的 IMEI 和 CDMA手机的 MEID. 如果设置ID不存在则取mac地址
     */
    public static String get_app_key(Context context) {
        String app_key = null;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null && tm.getDeviceId() != null) {
            app_key = tm.getDeviceId().toLowerCase(Locale.ENGLISH);
            if (app_key == null || app_key.trim().length() == 0) {
                app_key = StringUtils.getUUID();// 获取失败时appkey取UUID
            }
            // 处理部分手机获取的appkey为15位0的情况
            if (APPKEY_ZERO.equals(app_key)) {
                app_key = StringUtils.getUUID();
            }
        } else {
            app_key = StringUtils.getUUID();
        }

        return app_key;
    }

    /**
     * 获取id地址
     */
    public static String getUserIp() {
        String ipaddress = "";
        try {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements()) {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements()) {
                    InetAddress ip = inet.nextElement();
                    if (!ip.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(ip
                            .getHostAddress())) {
                        return ipaddress = ip.getHostAddress();
                    }
                }

            }
        } catch (SocketException e) {
            Log.e("TAG", "获取本地ip地址失败");
            e.printStackTrace();
        }
        return ipaddress;
    }
}
